package com.github.jinatonic.confetti.sample.slice;

import com.github.jinatonic.confetti.ConfettiManager;
import com.github.jinatonic.confetti.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    StackLayout container;
    private int[] colors;
    private int goldLight,goldDark;

    private final List<ConfettiManager> activeConfettiManagers = new ArrayList<>();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(getLayoutRes());

        ResourceManager resourceManager = getResourceManager();
        try {
            goldDark = resourceManager.getElement(ResourceTable.Color_gold_dark).getColor();
            int goldMed = resourceManager.getElement(ResourceTable.Color_gold_med).getColor();
            int gold = resourceManager.getElement(ResourceTable.Color_gold2).getColor();
            goldLight = resourceManager.getElement(ResourceTable.Color_gold_light).getColor();
            colors = new int[] {goldDark, goldMed, gold, goldLight};
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }

        container = (StackLayout) findComponentById(ResourceTable.Id_container);
        findComponentById(ResourceTable.Id_generate_confetti_once_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stream_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_infinite_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stop_btn).setClickedListener(this);
    }

    public int[] getColors() {
        return colors;
    }

    public int getGoldDark() {
        return goldDark;
    }

    public int getGoldLight() {
        return goldLight;
    }

    protected int getLayoutRes() {
        return ResourceTable.Layout_ability_confetti;
    }
    @Override
    public void onClick(Component component) {
        final int id = component.getId();
        if (id == ResourceTable.Id_generate_confetti_once_btn) {
            activeConfettiManagers.add(generateOnce());
        } else if (id == ResourceTable.Id_generate_confetti_stream_btn) {
            activeConfettiManagers.add(generateStream());
        } else if (id == ResourceTable.Id_generate_confetti_infinite_btn) {
            activeConfettiManagers.add(generateInfinite());
        } else {
            for (ConfettiManager confettiManager : activeConfettiManagers) {
                confettiManager.terminate();
            }
            activeConfettiManagers.clear();
        }
    }
    protected abstract ConfettiManager generateOnce();
    protected abstract ConfettiManager generateStream();
    protected abstract ConfettiManager generateInfinite();
}
